/*
******************************************************************************************************* 
**  Copyright (C) 2019, 苏州检易生物科技有限公司 
**  All rights reserved. 
** 
**  FileName:       	tcp_transfer.h
**  Description:	
**  Author:        	 
**  Version				v0.1
**  Date:           	20221017
**	Function List： 
**  History:         
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*                                              	防止重复包含
*********************************************************************************************************
*/

#ifndef _TCP_TRANSFER_H_
#define _TCP_TRANSFER_H_
/*
*********************************************************************************************************
*                                              	调试开关
*********************************************************************************************************
*/



/*
*********************************************************************************************************
*                                              	头文件
*********************************************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stm32h7xx_hal.h"
#include "eth_task.h"
#include "log_console.h"
#include "tcp_protocol.h"
/*
*********************************************************************************************************
*                                              	宏定义
*********************************************************************************************************
*/
#define		TRANSFER_HEAD		0xFA
#define		TRANSFER_TAIL		0xFB

#define		TRANSFER_PERIODIIC_INTERVAL	10
#define		TRANSFER_RESEND_MAX					3

#define		TRANSFER_SEND_LIST_MAX	16
#define		TRANSFER_SEND_SIZE_MAX	(256)

#define		TRANSFER_ACK_TIMEOUT		2000

#define 	TRANSFER_SEND_DATA_LEN(tot_len)		((tot_len)-sizeof(transfer_head_t)-sizeof(transfer_tail_t))
/*
*********************************************************************************************************
*                                              枚举和结构体类型定义
*********************************************************************************************************
*/
typedef enum{
	TRANSFER_ERR_NONE,
	TRANSFER_ERR_FORMAT,
	TRANSFER_ERR_TIMEOUT,
	TRANSFER_ERR_CRC,
	TRANSFER_ERR_FATAL,
	TRANSFER_ERR_BLOCKED,
}transfer_err_t;

typedef enum{
	TRANSFER_DISCONNECT,
	TRANSFER_WAIT_SYN,
	TRANSFER_IDLE,
	TRANSFER_WAIT_ACK,
}transfer_status_t;

typedef enum{
	/*机械相关故障*/
	MECH_ERR_STALL				=	0x41000000,	/*电机堵转*/
	MECH_ERR_PROBE_COL		=	0x41000001,	/*撞针*/
	MECH_ERR_PROBE_BLOCK	=	0x41000002,	/*堵针*/
	MECH_ERR_CLAW					=	0x41000003,	/*夹爪夹杯异常*/
	MECH_ERR_HEAT					=	0x42000000,	/*加热*/
	MECH_ERR_FREEZ				=	0x42000001,	/*制冷*/
	MECH_ERR_PMT_DARK			=	0x43000000,	/*PMT暗底*/
	MECH_ERR_SP_SURFACE		=	0x44000000,	/*样本针液面探测失败*/
	MECH_ERR_RP_SURFACE		=	0x44000001,	/*试剂针液面探测失败*/
	MECH_ERR_MP_SURFACE		=	0x44000002,	/*磁珠针液面探测失败*/
	
	/*电气相关故障*/
	ELEC_ERR_CAN_NODE			= 0x51000000,	/*CAN总线节点通讯异常*/
	ELEC_ERR_PMT_COUNTER	= 0x52000000,	/*计数器通讯错误*/
	ELEC_ERR_SCAN					= 0x52000001,	/*扫码枪通讯错误*/
	ELEC_ERR_RFID					= 0x52000002,	/*RFID通讯错误*/
	ELEC_ERR_SERVO				= 0x52000003,	/*伺服电机通讯错误*/
	ELEC_ERR_MOTOR				= 0x53000000,	/*电机错误*/
	ELEC_ERR_FLASH				= 0x54000000,	/*下位机FLASH错误*/
	ELEC_ERR_SP_SFINIT		= 0x55000000,	/*样本针液面探测初始化失败*/
	ELEC_ERR_RP_SFINIT		= 0x55000001,	/*试剂针液面探测初始化失败*/
	ELEC_ERR_MP_SFINIT		= 0x55000002,	/*磁珠针液面探测初始化失败*/
	
}product_err_code_t;

#pragma pack(1)
typedef struct{
	uint16_t id;
	uint32_t pos;
	uint32_t speed;
	uint16_t relative_pos; /*1表示增量位置*/
}jog_cmd_t;
typedef struct{
	uint16_t id;
	uint16_t open;
	uint32_t duty_cycle;
}pump_cmd_t;
typedef struct{
	uint16_t id;
	uint16_t status;
}led_cmd_t;
typedef struct TRANSFER_HEAD_T{
	uint8_t head;
	uint32_t win;
	uint8_t cmd;
	uint16_t length;
	uint32_t id;
}transfer_head_t;

typedef struct{
	uint16_t claw_id;
	uint16_t claw_status;
}claw_cmd_t;
typedef struct TRANSFER_TAIL_T{
	uint16_t modbus_crc;
	uint8_t tail;
}transfer_tail_t;
#pragma pack()

typedef struct TRANSFER_SEND_NODE{
	struct TRANSFER_SEND_NODE* next;
	protocol_cmd_t cmd;
	uint8_t		buff[TRANSFER_SEND_SIZE_MAX];
	uint16_t	size;
}transfer_send_node_t;

typedef struct{
	transfer_status_t status; /*发送状态*/
	uint32_t local_win;	/*下一条消息的win值*/
	uint32_t remote_win;
	
	struct rt_mempool transfer_send_mem;
	
	transfer_send_node_t* p_send_head;
	
	uint64_t time_cnt;		/*timer 计时*/
	uint64_t send_time;		/*上一次发送的时间点*/
	uint8_t  err_cnt;			/*发送次数*/
}transfer_cb_t;

/*
*********************************************************************************************************
*                                              	全局变量声明
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*                                              	函数声明
*********************************************************************************************************
*/
transfer_err_t tcp_transfer_init(void);
transfer_err_t tcp_transfer_push(protocol_cmd_t cmd,void* buff,uint16_t size);
transfer_err_t tcp_err_push(product_err_code_t err_code,const char * fmt,...);
#endif
